package com.fingard.dsp.bank.directbank.hxb02;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IRealTime;
import com.fingard.dsp.bank.directbank.hxb02.util.XMLDocument;
import com.fingard.dsp.bank.reqrespobj.single.ReqQrySingle;
import com.fingard.dsp.bank.reqrespobj.single.ReqSingle;
import com.fingard.dsp.bank.reqrespobj.single.RetAtsSingle;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 华夏E商宝单笔代扣
 * 
 * @author Cruise
 * @date 2018/12/11
 */
public class HXB02RealTime extends HXB02Base implements IRealTime {
	@Override
	public RetAtsSingle d_9108_RealTimeRec(ReqSingle p_req9108) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req9108.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		tmpRetDetail.reqSeqID = p_req9108.singleDetail.askNumber;
		
		try {
			HXB02Header tmpBankHeader = new HXB02Header();
			tmpBankHeader.reqSeqNo = p_req9108.reqHeader.transSeqID;
			tmpBankHeader.transCode = "MP009";
			tmpBankHeader.transTime = Format.DateTime14Format.format(new Date());

			XmlTextWriter xmlWriter = new XmlTextWriter();
			xmlWriter.autoFormat = false;
			writeCommonHeader(xmlWriter, tmpBankHeader);

			xmlWriter.writeStartElement("Body");
			if ("1".equals(p_req9108.singleDetail.isPrivate)) {
				xmlWriter.writeElementString("CustType", "0");
			} else {
				xmlWriter.writeElementString("CustType", "1");
			}
			xmlWriter.writeElementString("MerchantId", bankFront.corpNo);
			xmlWriter.writeElementString("TrnxType", "30");
			xmlWriter.writeElementClosed("SubMerchantId");
			xmlWriter.writeElementString("OrderNo", p_req9108.singleDetail.askNumber);
			xmlWriter.writeElementString("OrderAmt", StringHelper.amountMultiply100(p_req9108.singleDetail.payAmount));
			xmlWriter.writeElementString("Currency", "CNY");
			if (StringHelper.isNullOrEmpty(p_req9108.singleDetail.expiredDate) && StringHelper.isNullOrEmpty(p_req9108.singleDetail.cvv2)) {
				xmlWriter.writeElementString("CustName", p_req9108.singleDetail.payName);
			}
			
			// 卡种: 0：本行借记卡、1：本行信用卡、2:  他行卡
			String cardType = p_req9108.singleDetail.cardType;
			if ("1".equals(p_req9108.singleDetail.sameBnk)) {
				if (StringHelper.isNullOrEmpty(cardType) || "2".equals(cardType)) {
					cardType = "0";
				} else {
					cardType = "1";
				}
			} else {
				cardType = "2";
			}
			xmlWriter.writeElementString("CardType", cardType);
			xmlWriter.writeElementString("CardNo", p_req9108.singleDetail.payAct);
			if (StringHelper.isNullOrEmpty(p_req9108.singleDetail.expiredDate) && StringHelper.isNullOrEmpty(p_req9108.singleDetail.cvv2)) {
				xmlWriter.writeElementString("CertType", convertCertTypeToBank(p_req9108.singleDetail.certType));
				xmlWriter.writeElementString("CertNo", p_req9108.singleDetail.certNum);
			}
			if (StringHelper.hasAnyChar(p_req9108.singleDetail.cvv2)) {
				xmlWriter.writeElementString("CVN2", p_req9108.singleDetail.cvv2);
			} else {
				xmlWriter.writeElementClosed("CVN2");
			}
			
			String expiredDate = p_req9108.singleDetail.expiredDate;
			if (StringHelper.isNullOrEmpty(expiredDate)) {
				expiredDate = Format.Date4YearMonth.format(new Date());
			}
			xmlWriter.writeElementString("ValidDate", expiredDate);
			xmlWriter.writeElementClosed("Remark1");
			xmlWriter.writeElementClosed("Remark2");
			xmlWriter.writeEndDocument();
			
			String tmpStrRet = sign(xmlWriter.toXmlString(), getCharset());
			
			StringBuilder tmpSbMsg = new StringBuilder();
			XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet, tmpSbMsg);
			if (tmpBkReader != null) {
				String tmpRespCode = tmpBkReader.getFirstText("/html/head/result");
				if ("0".equals(tmpRespCode)) {
					// 签名成功
					tmpRetDetail.respCode = "0";
					String signature = tmpBkReader.getFirstText("/html/body/sign");
					
					tmpStrRet = sendToBank(signature, getCharset());
					
					tmpStrRet = verifysign(tmpStrRet, getCharset());
					
					XMLDocument xml = new XMLDocument(tmpStrRet);
					tmpRespCode = xml.getValueNoNull("result");
					if ("0".equals(tmpRespCode)) {
                        tmpStrRet = xml.getValueNoNull("sic");
						tmpBkReader = loadBankRespXmlReader(tmpStrRet, tmpSbMsg);
						if (tmpBkReader != null) {
							tmpRetDetail.respCode = "0";
							tmpRetDetail.respInfo = tmpBkReader.getFirstText("/HXE/Head/RespDescp");
							
							tmpRespCode = tmpBkReader.getFirstText("/HXE/Head/RespCode");
							String status = tmpBkReader.getFirstText("/HXE/Body/Status");
							if (StringHelper.hasAnyChar(status)) {
								tmpRetDetail.transState = "D+" + status;
								tmpRetDetail.payInfoCode = tmpRespCode;
							} else {
								tmpRetDetail.transState = tmpRespCode;
								tmpRetDetail.payInfoCode = tmpRespCode;
							}
						} else {
							tmpRetDetail.respCode = "-1";
							tmpRetDetail.respInfo = tmpSbMsg.toString();
						}
					} else {
						tmpRetDetail.respCode = "0";
						tmpRetDetail.respInfo = tmpBkReader.getFirstText("/html/body/p");
						tmpRetDetail.transState = PaymentState.FAIL_Payment;
						tmpRetDetail.payInfoCode = "E9981";
					}
				} else {
					tmpRetDetail.respCode = "0";
					tmpRetDetail.respInfo = tmpBkReader.getFirstText("/html/body/p");
					tmpRetDetail.transState = PaymentState.FAIL_Payment;
					tmpRetDetail.payInfoCode = "E9981";
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpSbMsg.toString();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.toString();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingle d_9144_QryRealTimeRec(ReqQrySingle p_req9144) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req9144.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail();
		tmpRetDetail.reqSeqID = p_req9144.askNumber;
		
		try {
			HXB02Header tmpBankHeader = new HXB02Header();
			tmpBankHeader.reqSeqNo = p_req9144.reqHeader.transSeqID;
			tmpBankHeader.transCode = "MQ011";
			tmpBankHeader.transTime = Format.DateTime14Format.format(new Date());

			XmlTextWriter xmlWriter = new XmlTextWriter();
			xmlWriter.autoFormat = false;
			writeCommonHeader(xmlWriter, tmpBankHeader);

			xmlWriter.writeStartElement("Body");
			xmlWriter.writeElementString("MerchantId", bankFront.corpNo);
			xmlWriter.writeElementString("SearchType", "0");
			xmlWriter.writeElementString("OrderNo", p_req9144.askNumber);
			xmlWriter.writeElementString("MerSerNo", "");
			xmlWriter.writeElementString("TrnxType", "30");
			xmlWriter.writeElementString("Remark1", "");
			xmlWriter.writeElementString("Remark2", "");
			xmlWriter.writeElementString("Remark3", "");
			xmlWriter.writeElementString("Remark4", "");
			xmlWriter.writeElementString("Remark5", "");
			xmlWriter.writeEndDocument();
		
			String tmpStrRet = sign(xmlWriter.toXmlString(), getCharset());
			
			StringBuilder tmpSbMsg = new StringBuilder();
			XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet, tmpSbMsg);
			if (tmpBkReader != null) {
				String tmpRespCode = tmpBkReader.getFirstText("/html/head/result");
				if ("0".equals(tmpRespCode)) {
					// 签名成功
					tmpRetDetail.respCode = "0";
					String signature = tmpBkReader.getFirstText("/html/body/sign");
					
					tmpStrRet = sendToBank(signature, getCharset());
					
					tmpStrRet = verifysign(tmpStrRet, getCharset());
					
					XMLDocument xml = new XMLDocument(tmpStrRet);
					tmpRespCode = xml.getValueNoNull("result");
					if ("0".equals(tmpRespCode)) {
						tmpStrRet = xml.getValueNoNull("sic");
						tmpBkReader = loadBankRespXmlReader(tmpStrRet, tmpSbMsg);
						if (tmpBkReader != null) {
							tmpRetDetail.respCode = "0";
							tmpRetDetail.respInfo = tmpBkReader.getFirstText("/HXE/Head/RespDescp");
							
							tmpRespCode = tmpBkReader.getFirstText("/HXE/Head/RespCode");
							if ("000000".equals(tmpRespCode)) {
								tmpRetDetail.transAmt = StringHelper.amountDivide100(tmpBkReader.getFirstText("/HXE/Body/OrderAmt"));
								tmpRetDetail.transState = StringHelper.joinNotEmpty("+", tmpRespCode, tmpBkReader.getFirstText("/HXE/Body/Status"));
								tmpRetDetail.payInfoCode = tmpRetDetail.transState;
							}else {
								if ("ESB201".equals(tmpRespCode)) {
									tmpRetDetail.transState = tmpRespCode;
									tmpRetDetail.payInfoCode = tmpRespCode;
								} else {
									tmpRetDetail.respCode = "-1";
									tmpRetDetail.respInfo = tmpBkReader.getFirstText("/HXE/Head/RespDescp");
								}
							}
						} else {
							tmpRetDetail.respCode = "-1";
							tmpRetDetail.respInfo = tmpSbMsg.toString();
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = "验签失败:" + tmpBkReader.getFirstText("/html/body/p");
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = "签名失败:" + tmpBkReader.getFirstText("/html/body/p");
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpSbMsg.toString();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}
}
